package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     *  添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断购物车中是否有该商品
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart = shoppingCartMapper.get(shoppingCart);
        //如果有，则数量加一
        if (shoppingCart != null){
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.update(shoppingCart);
            return;
        }

        //如果没有，则添加到购物车，数量默认为1
        shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setNumber(1);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setCreateTime(LocalDateTime.now());
        //判断该商品是菜品还是套餐
        if (shoppingCartDTO.getDishId() != null) {
            //是菜品
            Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());

        } else {
            //是套餐
            SetmealVO setmealVO = setmealMapper.getById(shoppingCartDTO.getSetmealId());
            shoppingCart.setName(setmealVO.getName());
            shoppingCart.setImage(setmealVO.getImage());
            shoppingCart.setAmount(setmealVO.getPrice());
        }
        shoppingCartMapper.insert(shoppingCart);
    }

    /**
     *  查询购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        return shoppingCartMapper.getAll(BaseContext.getCurrentId());
    }

    /**
     *  清空购物车
     */
    @Override
    public void clean() {
        shoppingCartMapper.cleanAll(BaseContext.getCurrentId());
    }

    /**
     *  减少购物车中一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart = shoppingCartMapper.get(shoppingCart);
        //判断该商品的数量是否为1
        if (shoppingCart.getNumber() == 1){
            //如果为1，则删除该商品
            shoppingCartMapper.deleteById(shoppingCart.getId());
            return;
        }
        //如果不为1，则数量减一
        shoppingCart.setNumber(shoppingCart.getNumber() - 1);
        shoppingCartMapper.update(shoppingCart);
    }
}
